package com.google.code.mochaccino.framework.hibernate.exception;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ValidationExceptionList extends RuntimeException {

	private static final long                      serialVersionUID = 1L;
	private              List<ValidationException> exceptions       = null;

	/** Constructor */
	public ValidationExceptionList() {
		super();
		exceptions = new ArrayList<ValidationException>();
	}

	/**
	 * Constructor
	 *
	 * @param msg Message for this ValidationException List
	 */
	public ValidationExceptionList( String msg ) {
		super( msg );
		exceptions = new ArrayList<ValidationException>();
	}

	/**
	 * Constructor
	 *
	 * @param msg    Message for this ValidationException List
	 * @param exList ValidationException Collection
	 */
	public ValidationExceptionList( String msg, Collection<ValidationException> exList ) {
		super( msg );
		exceptions = new ArrayList<ValidationException>( exList );
	}

	/**
	 * Constructor
	 *
	 * @param msg    Message for this ValidationException List
	 * @param exList ValidationException Array
	 */
	public ValidationExceptionList( String msg, ValidationException[] exList ) {
		super( msg );
		exceptions = new ArrayList<ValidationException>();
		add( exList );
	}

	/** Add ValidationException Array to List */
	public void add( ValidationException[] exList ) {
		for ( ValidationException ex : exList ) {
			add( ex );
		}
	}

	/**
	 * Add ValidationException to List
	 *
	 * @param ex The ValidationException
	 */
	public void add( ValidationException ex ) {
		exceptions.add( ex );
	}

	/* (non-Javadoc)
	 * @see java.lang.ValidationException#printStackTrace()
	 */
	@Override
	public void printStackTrace() {
		super.printStackTrace();
		for ( int I = 0; I < exceptions.size(); I++ ) {
			ValidationException ex = exceptions.get( I );
			ex.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see java.lang.ValidationException#printStackTrace(java.io.PrintStream)
	 */
	@Override
	public void printStackTrace( PrintStream pStream ) {
		super.printStackTrace( pStream );
		for ( int I = 0; I < exceptions.size(); I++ ) {
			ValidationException ex = exceptions.get( I );
			ex.printStackTrace( pStream );
		}
	}

	/* (non-Javadoc)
	 * @see java.lang.ValidationException#printStackTrace(java.io.PrintWriter)
	 */
	@Override
	public void printStackTrace( PrintWriter pWriter ) {
		super.printStackTrace( pWriter );
		for ( int I = 0; I < exceptions.size(); I++ ) {
			ValidationException ex = exceptions.get( I );
			ex.printStackTrace( pWriter );
		}
	}

	/** Clear the list */
	public void clear() {
		exceptions.clear();
	}

	/**
	 * Return a ValidationException for specified index
	 *
	 * @return ValidationException
	 */
	public ValidationException get( int idx ) {
		return exceptions.get( idx );
	}

	/**
	 * Remove ValidationException from Specified Index
	 *
	 * @param idx Index of the ValidationException
	 */
	public void remove( int idx ) {
		exceptions.remove( idx );
	}

	/** @return The count of exception in the list */
	public int size() {
		return exceptions.size();
	}
}